package com.define.ssh.action.monitoring;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.define.ssh.action.admin.BaseAdminAction;
import com.define.ssh.common.JsonUtil;
import com.define.ssh.model.GroupinfoRegular;
import com.define.ssh.model.GroupmemberRegular;
import com.define.ssh.model.MWarnLevel;
import com.define.ssh.model.TbLog;
import com.define.ssh.model.UserInfo;
import com.define.ssh.service.DeptuserService;
import com.define.ssh.service.MWarnLevelService;
import com.define.ssh.service.TbLogService;
import com.define.ssh.util.SystemUtil;
import com.define.ssh.util.Pager.Order;

/**
 *  
 * 
 */
@ParentPackage("admin")
public class ConfigAction extends BaseAdminAction {
	
	private static final long serialVersionUID = 4L;
	/** 页面传递参数过来*/
	protected int pageSize;
	protected int pageNum;

	@Resource(name = "mwarnLevelServiceImpl")
	private MWarnLevelService mwarnLevelService;
	/**
	 * 列表页
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() {
		
		try {
			
	
		HashMap<String, Object> searchMap = new HashMap<String, Object>();
		pager.setOrder(Order.asc);
		pager.setOrderBy("id");
		pager.setSearchMap(searchMap);
		pager =mwarnLevelService.findPager(pager);
		} catch (Exception e) {
			e.printStackTrace();
		}	
		
		return "config";
	}
	
	
	/**
	 * 保存
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() {
		
		
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");  
		response.setCharacterEncoding("UTF-8");
		String warnlevel = super.getRequest().getParameter("warnlevel");//告警级别
		String warndetail = super.getRequest().getParameter("warndetail");
		try {
		
			 DetachedCriteria dt = DetachedCriteria.forClass(MWarnLevel.class);	
			 dt.add(Restrictions.eq("warnlevel",Integer.valueOf(warnlevel)));
			 List<MWarnLevel> userlist=mwarnLevelService.findByCriteria(dt);
			if(userlist!=null&&userlist.size()>0){
				
				response.getWriter().write("告警级别已存在请重新输入!");
				return null;
			}
			Date updatetime = mwarnLevelService.getUpdateFiled("m_warn_level","updatetime");
			MWarnLevel mwl=new MWarnLevel();
			mwl.setWarnlevel(Integer.valueOf(warnlevel));
			mwl.setWarndetail(warndetail);
			mwl.setUpdatetype(1);////更新标示 1新增  2修改  3删除
			mwl.setUpdateTime(new Date());
			mwarnLevelService.save(mwl);
		   
			response.getWriter().write("修改成功");
			Long modfiytime = updatetime.getTime() / 1000;
			mwarnLevelService.connectInterfce("m_warn_level", 2001,modfiytime.intValue());
			   
		
		} catch (Exception e) {
			e.printStackTrace();
		}	
		
		return null;
	}
	
	/**
	 *详情
	 * @return
	 */
	public String mwarnLevelInfo() {
		
		try {
			String warnlevel = super.getRequest().getParameter("warnlevel");
			 DetachedCriteria dt = DetachedCriteria.forClass(MWarnLevel.class);	
			 dt.add(Restrictions.eq("warnlevel",Integer.valueOf(warnlevel)));
			 List<MWarnLevel> userlist=mwarnLevelService.findByCriteria(dt);
			 if(userlist!=null&&userlist.size()>0){
				 MWarnLevel mwl= userlist.get(0);
			     return ajax(JsonUtil.beanToJson(mwl));
			 }
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/****
	 * 修改
	 */
	public String update() {
		
		try {
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");  
		response.setCharacterEncoding("UTF-8");
		
		String warnlevel = super.getRequest().getParameter("warnlevel");
		String warndetail = super.getRequest().getParameter("warndetail");
		 DetachedCriteria dt = DetachedCriteria.forClass(MWarnLevel.class);	
		 dt.add(Restrictions.eq("warnlevel",Integer.valueOf(warnlevel)));
		 List<MWarnLevel> userlist=mwarnLevelService.findByCriteria(dt);
		 Date updatetime = mwarnLevelService.getUpdateFiled("m_warn_level","updatetime");
		 if(userlist!=null&&userlist.size()>0){
			 MWarnLevel mwl= userlist.get(0);
			 mwl.setUpdatetype(1);////更新标示 1新增  2修改  3删除
			 mwl.setUpdateTime(new Date());
			 mwl.setWarnlevel(Integer.valueOf(warnlevel));
			 mwl.setWarndetail(warndetail);
			 mwarnLevelService.update(mwl);
			
			 response.getWriter().write("true");
			 Long modfiytime = updatetime.getTime() / 1000;
			 mwarnLevelService.connectInterfce("m_warn_level", 2001,modfiytime.intValue());
		 }
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/****
	 * 删除
	 */
	public String delete() {
		
		try {
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");  
		response.setCharacterEncoding("UTF-8");
		
		String warnlevel = super.getRequest().getParameter("warnlevel");
		 DetachedCriteria dt = DetachedCriteria.forClass(MWarnLevel.class);	
		 dt.add(Restrictions.eq("warnlevel",Integer.valueOf(warnlevel)));
		 List<MWarnLevel> userlist=mwarnLevelService.findByCriteria(dt);
		 Date updatetime = mwarnLevelService.getUpdateFiled("m_warn_level","updatetime");
		 if(userlist!=null&&userlist.size()>0){
			 MWarnLevel mwl= userlist.get(0);
			 mwarnLevelService.delete(mwl);
			 response.getWriter().write("true");
			 Long modfiytime = updatetime.getTime() / 1000;
			  mwarnLevelService.connectInterfce("m_warn_level", 2001,modfiytime.intValue());
		 }
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}